package com.shengyin.ec.controller.permission;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.shengyin.ec.model.entity.Resources;
import com.shengyin.ec.service.permission.ResourcesService;

@Controller
public class ResourcesController {

	
	@Autowired
	ResourcesService resourcesService;
	/*
	 * 页面跳转 start =============================================================
	 */
	@RequestMapping(value = "/admin2/permission/toResourcesAdd.do")
	public String toResourcesAdd() {
		return "redirect:resourcesAdd.jsp";
	}

	@RequestMapping(value = "/admin2/permission/toResourcesList.do")
	public String toResourcesList(Model model) {
		List<Resources> resources=resourcesService.getResourcesList(0, 1000);
		model.addAttribute("resourceList", resources);
		return "/admin2/permission/resourcesList";
	}

	@RequestMapping(value = "/admin2/permission/toResourcesModify.do")
	public String toResourcesModify(Model model,@RequestParam(value="id")long id) {
		Resources resources=resourcesService.getResourcesById(id);
		model.addAttribute("resource", resources);
		return "/admin2/permission/resourcesModify";
	}

	/*
	 * 
	 * =============================================================
	 */

	@RequestMapping(value="/admin2/permission/resourcesAdd.do")
	public String ResourceAdd(ResourcesForm resourcesForm) {
		
		Resources resources =new Resources();
		
		try {
			BeanUtils.copyProperties(resources, resourcesForm);
			resourcesService.createResources(resources);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
		return "redirect:toResourcesAdd.do";
	}

	

	@RequestMapping(value="/admin2/permission/resourcesList.do")
	public String ResourceList(Model model) {
		List<Resources> resources=resourcesService.getResourcesList(0, 1000);
		model.addAttribute("resourceList", resources);
		return "/admin2/permission/resourcesList";
	}
	
	
	@RequestMapping(value="/admin2/permission/resourcesDelete.do")
	public String ResourceDelete(@RequestParam(value="id")long id,Model model) {
		resourcesService.deleteResourcesById(id);
		return "redirect:toResourcesList.do";
	}
	
	
	@RequestMapping(value="/admin2/permission/resourcesModify.do")
	public String ResourceModify(Model model,ResourcesForm resourcesForm) {
		Resources resources =new Resources();
		try {
			BeanUtils.copyProperties(resources, resourcesForm);
			resourcesService.modifyResources(resources);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
		return "redirect:toResourcesList.do";
	}
}
